package com.rocpenghua.ocr;

import org.apache.tika.Tika;
import org.apache.tika.exception.TikaException;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.asm.ClassParser;
import org.apache.tika.parser.html.HtmlParser;
import org.apache.tika.parser.image.ImageParser;
import org.apache.tika.parser.jpeg.JpegParser;
import org.apache.tika.parser.microsoft.OfficeParserConfig;
import org.apache.tika.parser.microsoft.ooxml.OOXMLParser;
import org.apache.tika.parser.mp4.MP4Parser;
import org.apache.tika.parser.pdf.PDFParser;
import org.apache.tika.parser.pkg.PackageParser;
import org.apache.tika.parser.txt.TXTParser;
import org.apache.tika.sax.BodyContentHandler;
import org.xml.sax.SAXException;

import java.io.FileInputStream;
import java.io.IOException;

/**
 * @author pengp
 * @date 2024年02月22日 17:57
 */
public class TestTikaParse {
    public static void main(String[] args) throws TikaException, IOException, SAXException {
        String filePath = "E://soft//ocr//tika//heukms-debug.txt";
        filePath = "E://soft//ocr//tika//现场检查记录登记表.pdf";
        final String s = parsePdfImg(filePath);

    }
    public static String parseTxt(String filePath){
        try {
            BodyContentHandler handler = new BodyContentHandler();
            Metadata metadata = new Metadata();
            FileInputStream inputstream = new FileInputStream(filePath);
            ParseContext pcontext=new ParseContext();
            //Text document parser
            TXTParser txTParser = new TXTParser();
            txTParser.parse(inputstream, handler, metadata,pcontext);
            String text = handler.toString();
            System.out.println("Contents of the document:");
            System.out.println(text);
            System.out.println("Metadata of the document:");
            String[] metadataNames = metadata.names();

            for(String name : metadataNames) {
                System.out.println(name + ": " + metadata.get(name));
            }
            return text;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
    public static String parsePdf(String filePath){
        try {
            BodyContentHandler handler = new BodyContentHandler();
            Metadata metadata = new Metadata();
            FileInputStream inputstream = new FileInputStream(filePath);
            ParseContext pcontext = new ParseContext();
            //parsing the document using PDF parser
            PDFParser pdfparser = new PDFParser();
            pdfparser.parse(inputstream, handler, metadata,pcontext);
            //getting the content of the document
            String text = handler.toString();
            System.out.println("Contents of the document:");
            System.out.println(text);
            System.out.println("Metadata of the document:");
            String[] metadataNames = metadata.names();

            for(String name : metadataNames) {
                System.out.println(name + ": " + metadata.get(name));
            }
            return text;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
    public static String parseExcel(String filePath){
        try {
            BodyContentHandler handler = new BodyContentHandler();
            Metadata metadata = new Metadata();
            FileInputStream inputstream = new FileInputStream(filePath);
            ParseContext pcontext=new ParseContext();
            OOXMLParser msofficeparser = new OOXMLParser ();
            msofficeparser.parse(inputstream, handler, metadata,pcontext);
            String text = handler.toString();
            System.out.println("Contents of the document:");
            System.out.println(text);
            System.out.println("Metadata of the document:");
            String[] metadataNames = metadata.names();

            for(String name : metadataNames) {
                System.out.println(name + ": " + metadata.get(name));
            }
            return text;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
    public static String parseWord(String filePath){
        ParseContext parseContext = new ParseContext();
        AutoDetectParser parser = new AutoDetectParser();
        BodyContentHandler handler = new BodyContentHandler();
        Metadata metadata = new Metadata();

        //跳过页眉页脚
        OfficeParserConfig officeParserConfig = new OfficeParserConfig();
        officeParserConfig.setIncludeHeadersAndFooters(false);
        boolean hf= officeParserConfig.getIncludeHeadersAndFooters();
        parseContext.set(OfficeParserConfig.class, officeParserConfig);

        System.out.println("headfoot"+hf);
        try(FileInputStream fin=new FileInputStream(filePath)) {
            parser.parse(fin, handler, metadata, parseContext);
            String text = handler.toString();
            System.out.println("Contents of the document:");
            System.out.println(text);
            System.out.println("Metadata of the document:");
            String[] metadataNames = metadata.names();

            for(String name : metadataNames) {
                System.out.println(name + ": " + metadata.get(name));
            }
            return text;
        } catch (IOException | SAXException | TikaException ex) {
            ex.printStackTrace();
        }
        return "";
    }
    public static String parseHtml(String filePath){
        BodyContentHandler handler = new BodyContentHandler();
        Metadata metadata = new Metadata();
        try(FileInputStream fin=new FileInputStream(filePath)) {
            ParseContext pcontext = new ParseContext();

            HtmlParser msofficeparser = new HtmlParser();
            msofficeparser.parse(fin, handler, metadata,pcontext);
            String text = handler.toString();
            System.out.println("Contents of the document:");
            System.out.println(text);
            System.out.println("Metadata of the document:");
            String[] metadataNames = metadata.names();

            for(String name : metadataNames) {
                System.out.println(name + ": " + metadata.get(name));
            }
            return text;
        } catch (IOException | SAXException | TikaException ex) {
            ex.printStackTrace();
        }
        return "";
    }

    public static String parseClass(String filePath){
        BodyContentHandler handler = new BodyContentHandler();
        Metadata metadata = new Metadata();
        try(FileInputStream fin=new FileInputStream(filePath)) {
            ParseContext pcontext = new ParseContext();

            ClassParser parser= new ClassParser();
            parser.parse(fin, handler, metadata,pcontext);
            String text = handler.toString();
            System.out.println("Contents of the document:");
            System.out.println(text);
            System.out.println("Metadata of the document:");
            String[] metadataNames = metadata.names();

            for(String name : metadataNames) {
                System.out.println(name + ": " + metadata.get(name));
            }
            return text;
        } catch (IOException | SAXException | TikaException ex) {
            ex.printStackTrace();
        }
        return "";
    }

    public static String parseJar(String filePath){
        BodyContentHandler handler = new BodyContentHandler();
        Metadata metadata = new Metadata();
        try(FileInputStream fin=new FileInputStream(filePath)) {
            ParseContext pcontext = new ParseContext();

            PackageParser parser= new PackageParser();
            parser.parse(fin, handler, metadata,pcontext);
            String text = handler.toString();
            System.out.println("Contents of the document:");
            System.out.println(text);
            System.out.println("Metadata of the document:");
            String[] metadataNames = metadata.names();

            for(String name : metadataNames) {
                System.out.println(name + ": " + metadata.get(name));
            }
            return text;
        } catch (IOException | SAXException | TikaException ex) {
            ex.printStackTrace();
        }
        return "";
    }

    public static String parseJpeg(String filePath){
        BodyContentHandler handler = new BodyContentHandler();
        Metadata metadata = new Metadata();
        try(FileInputStream fin=new FileInputStream(filePath)) {
            ParseContext pcontext = new ParseContext();

            JpegParser parser= new JpegParser();

            parser.parse(fin, handler, metadata,pcontext);
            String text = handler.toString();
            System.out.println("Contents of the document:");
            System.out.println(text);
            System.out.println("Metadata of the document:");
            String[] metadataNames = metadata.names();

            for(String name : metadataNames) {
                System.out.println(name + ": " + metadata.get(name));
            }
            return text;
        } catch (IOException | SAXException | TikaException ex) {
            ex.printStackTrace();
        }
        return "";
    }
    public static String parseImage(String filePath){
        BodyContentHandler handler = new BodyContentHandler();
        Metadata metadata = new Metadata();
        try(FileInputStream fin=new FileInputStream(filePath)) {
            ParseContext pcontext = new ParseContext();

            ImageParser parser= new ImageParser();

            parser.parse(fin, handler, metadata,pcontext);
            String text = handler.toString();
            System.out.println("Contents of the document:");
            System.out.println(text);
            System.out.println("Metadata of the document:");
            String[] metadataNames = metadata.names();

            for(String name : metadataNames) {
                System.out.println(name + ": " + metadata.get(name));
            }
            return text;
        } catch (IOException | SAXException | TikaException ex) {
            ex.printStackTrace();
        }
        return "";
    }
    public static String parseMp4(String filePath){
        BodyContentHandler handler = new BodyContentHandler();
        Metadata metadata = new Metadata();
        try(FileInputStream fin=new FileInputStream(filePath)) {
            ParseContext pcontext = new ParseContext();

            MP4Parser parser= new MP4Parser();

            parser.parse(fin, handler, metadata,pcontext);
            String text = handler.toString();
            System.out.println("Contents of the document:");
            System.out.println(text);
            System.out.println("Metadata of the document:");
            String[] metadataNames = metadata.names();

            for(String name : metadataNames) {
                System.out.println(name + ": " + metadata.get(name));
            }
            return text;
        } catch (IOException | SAXException | TikaException ex) {
            ex.printStackTrace();
        }
        return "";
    }

    public static String parsePdfImg(String filePath){
        BodyContentHandler handler = new BodyContentHandler();
        Metadata metadata = new Metadata();
        try(FileInputStream fin=new FileInputStream(filePath)) {
            AutoDetectParser parser = new AutoDetectParser();
            ParseContext context = new ParseContext();
            parser.parse(fin, handler, metadata, context);
            String text = handler.toString();
            System.out.println("Contents of the document:");
            System.out.println(text);
            System.out.println("Metadata of the document:");
            String[] metadataNames = metadata.names();

            for(String name : metadataNames) {
                System.out.println(name + ": " + metadata.get(name));
            }
            return text;
        } catch (IOException | SAXException | TikaException ex) {
            ex.printStackTrace();
        }
        return "";
    }
    public static String detect(String filePath){
        Tika tika = new Tika();
        final String fileType = tika.detect(filePath);
        System.out.println(fileType);
        return fileType;
    }
}
